package com.sigmatrix.h5.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 *@ClassName: HttpClientUtils
 *@Description: 网络请求工具类
 *@author MH
 *@date 2017年9月26日 上午9:27:05
 */
public class HttpClientUtils {
	static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
	private static HttpClient httpClient;
	static{
		httpClient = HttpClients.createDefault();
	}
	/**
	 * 
	 *@Title: getBytes
	 *@Description: get 请求返回byte数组
	 *@return byte[]
	 *@author MH
	 *@date 2017年9月26日 上午10:00:50
	 *@param url
	 *@param params
	 *@return
	 */
	public static byte[] getBytes(String url,Map<String,Object> args){
		HttpResponse response = executeGet(url, args);
		if(response == null) return null;
		try {
			return EntityUtils.toByteArray(response.getEntity());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 
	 *@Title: getString
	 *@Description: get请求返回String
	 *@return String
	 *@author MH
	 *@date 2017年9月26日 上午10:01:21
	 *@param url
	 *@param params
	 *@return
	 */
	public static String getString(String url,Map<String,Object> args){
		HttpResponse response = executeGet(url, args);
		if(response == null) return null;
		try {
			return EntityUtils.toString(response.getEntity(),"UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 
	 *@Title: postString
	 *@Description: post请求返回String
	 *@return String
	 *@author MH
	 *@date 2017年9月26日 上午10:28:57
	 *@param url
	 *@param args
	 *@return
	 */
	public static String postString(String url,Map<String,Object> args){
		HttpResponse response = executePost(url, args);
		if(response == null) return null;
		try {
			return EntityUtils.toString(response.getEntity(),"UTF-8");
		} catch (ParseException | IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private static HttpResponse executeGet(String url,Map<String,Object> args){
		StringBuffer sb = new StringBuffer(url);
		//拼接参数
		if(CommonUtils.isNotEmpty(args)){
			if(sb.indexOf("?") == -1)sb.append("?1=1");
			for(Map.Entry<String, Object> entry : args.entrySet()){
				sb.append("&").append(entry.getKey()).append("=").append(entry.getValue());
			}
		}
		logger.info("get url :{}",sb);
		HttpGet get = new HttpGet(sb.toString());
		try {
			return httpClient.execute(get);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static HttpResponse executePost(String url,Map<String,Object> args){
		//处理参数
		String argsString = JSONObject.toJSONString(args);
		HttpPost post = new HttpPost(url);
		if(CommonUtils.isNotEmpty(args)){
			StringEntity entity = new StringEntity(argsString, ContentType.APPLICATION_JSON);
			post.setEntity(entity);
		}
		try {
			logger.info("post url :{}",url);
			logger.info("post 参数     :{}",argsString);
			return httpClient.execute(post);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) {
		HashMap<String,Object> map1 = new HashMap<String,Object>();
		map1.put("appid", "wx4fddc43e084624df");
		map1.put("grant_type", "client_credential");
		map1.put("secret", "f481d4149272f0d9e2d1936bca32bb9b");
		String result = HttpClientUtils.getString("https://api.weixin.qq.com/cgi-bin/token", map1);
		String access_token = JSONObject.parseObject(result).get("access_token")+"";
		String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token="+access_token;
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("touser", "otBAk0fPJW2AITIsWQmnkU2BpHAk");
		map.put("form_id", "380b698fbc3dc692cb7285f781414493");
		map.put("template_id", "1UhULLdlAg_Y-3prwHeCQY9f7ZyBv5lGzjhuzPR1sa4");
		//map.put("data", new SmallProgramTitle("处理成功", "无", new Date()));
	//	map.put("page", "otBAk0fPJW2AITIsWQmnkU2BpHAk");
		String r = postString(url, map);
		System.out.println(r);
	}
}
